package top.mcwebsite.playandroid.business.home.impl.ui

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.viewmodel.compose.viewModel
import top.mcwebsite.lib.route.api.EasyRouter
import top.mcwebsite.lib.router.annotation.Route
import top.mcwebsite.playandroid.business.gongzhonghao.api.GongZhongHaoService
import top.mcwebsite.playandroid.business.home.impl.vm.MainIntent
import top.mcwebsite.playandroid.business.home.impl.MainTabItem
import top.mcwebsite.playandroid.business.home.impl.mainTabItems
import top.mcwebsite.playandroid.home.ui.HomePage
import top.mcwebsite.playandroid.business.home.impl.vm.MainUiEffect
import top.mcwebsite.playandroid.business.home.impl.vm.MainViewModel
import top.mcwebsite.playandroid.business.project.api.ProjectService
import top.mcwebsite.playandroid.business.tutorial.api.ITutorialService
import top.mcwebsite.playandroid.business.user_article.api.UserArticleService
import top.mcwebsite.playandroid.common.constant.RouteConfig
import top.mcwebsite.playandroid.lib.servicemanager.core.ServiceManager

@Route("home")
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun MainPage() {
    val viewModel: MainViewModel = viewModel()
    viewModel.HandleUiEffect {
        when (it) {
            is MainUiEffect.GoNotificationUiEffect -> {
                EasyRouter.navigate(RouteConfig.Message.PATH)
            }
            is MainUiEffect.GoSearchUiEffect -> {
                EasyRouter.navigate(RouteConfig.Search.PATH)
            }
            is MainUiEffect.NavigationToLoginUiEffect -> {
                EasyRouter.navigate(RouteConfig.Login.PATH)
            }
        }
    }
    val uiState by viewModel.uiStateFlow.collectAsState()
    val horizontalPagerState = rememberPagerState(
        initialPage = 0
    ) {
        mainTabItems.size
    }
    LaunchedEffect(uiState.currentTab) {
        horizontalPagerState.scrollToPage(mainTabItems.indexOf(uiState.currentTab))
    }

    CollapsingLayout(modifier = Modifier,
        header = {
            CollapsingHeader(collapsingState = it, viewModel = viewModel)
        },
        content = {
            LaunchedEffect(Unit) {
                snapshotFlow {
                    horizontalPagerState.currentPage
                }.collect {
                    viewModel.sendIntent(MainIntent.OnHorizontalScrolledIntent(mainTabItems[it]))
                }
            }
            HorizontalPager(
                state = horizontalPagerState, modifier = Modifier
            ) { index ->
                Box(modifier = Modifier) {
                    when (mainTabItems[index]) {
                        MainTabItem.Home -> {
                            HomePage()
                        }

                        MainTabItem.Square -> {
                            ServiceManager.getService<UserArticleService>()
                                .UserArticlesPage()
                        }

                        MainTabItem.Tutorial -> {
                            ServiceManager.getService<ITutorialService>().TutorialContent()
                        }

                        MainTabItem.Project -> {
                            ServiceManager.getService<ProjectService>().ProjectPage()
                        }
                        MainTabItem.GongZhongHao -> {
                            ServiceManager.getService<GongZhongHaoService>().GongZhongHaoPage()
                        }

                        else -> {
                            Text(text = stringResource(id = mainTabItems[index].strRes))
                        }
                    }
                }
            }
        }
    )
}
